package org.compiere.model;

import java.io.File;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.apache.poi.hslf.model.Line;
import org.compiere.process.DocAction;
import org.compiere.process.DocumentEngine;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class MBCSACRInputFormIntCompany extends X_BCS_ACRInputForm_IntCompany {

	private static CLogger s_log = CLogger.getCLogger(MBCSACRInputFormIntCompany.class);
	
	public MBCSACRInputFormIntCompany(Properties ctx, int MBCSACRInputFormCompany_ID,
			String trxName) {
		super(ctx, MBCSACRInputFormCompany_ID, trxName);
		// TODO Auto-generated constructor stub
	}
	
	public MBCSACRInputFormIntCompany(Properties ctx, ResultSet rs, String trxName){
		super(ctx, rs, trxName);
	}

	protected boolean afterSave(boolean newRecord, boolean success){
		
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		boolean is = false;
		String sql = " SELECT * FROM bcs_acrinputformvgoo2 where BCS_ACRInputForm_IntCompany_ID = "  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{
			List<Integer> list = new ArrayList<Integer>();
	
			sql = 	"select BCS_ACRPositionsDict_ID from BCS_ACRSetupInFormVGOO2 order by BCS_ACRSetupInFormVGOO2_ID ";
	
				try{
					pstmt = DB.prepareStatement(sql,null);				
					rs = pstmt.executeQuery();	
					while(rs.next()){
						list.add(rs.getInt(1));
					}
				}catch (SQLException e){
					s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
				}finally{
					DB.close(rs, pstmt);
					rs = null; pstmt = null;
				}			
	
				for(Integer i: list){
					MBCSACRInputFormVGoo2 vg = new MBCSACRInputFormVGoo2(Env.getCtx(), 0, null);
					vg.setBCS_ACRInputForm_IntCompany_ID(get_ID());
					vg.setBCS_ACRPositionsDict_ID(i);
					vg.setAD_Org_ID(getAD_Org_ID());
					vg.save();
				
				}	
		}
		
		//vgoo 3
		is = false;
		sql = " SELECT * FROM bcs_acrinputformvgoo3 where BCS_ACRInputForm_IntCompany_ID = "  +  get_ID();

			try{
				pstmt = DB.prepareStatement(sql,null);				
				rs = pstmt.executeQuery();	
				if(rs.next()){
					is = true;
				}
			}catch (SQLException e){
				s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
			}finally{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}			

		if(is);	//if there are record then do nothing
		else{
			List<Integer> list = new ArrayList<Integer>();
	
			sql = 	"select BCS_ACRPositionsDict_ID from BCS_ACRSetupInFormVGOO3 order by BCS_ACRSetupInFormVGOO3_ID ";
	
				try{
					pstmt = DB.prepareStatement(sql,null);				
					rs = pstmt.executeQuery();	
					while(rs.next()){
						list.add(rs.getInt(1));
					}
				}catch (SQLException e){
					s_log.log(Level.SEVERE, "MBCSACRInputFormCompany", e);
				}finally{
					DB.close(rs, pstmt);
					rs = null; pstmt = null;
				}			
	
				for(Integer i: list){
					MBCSACRInputFormVGoo3 vg = new MBCSACRInputFormVGoo3(Env.getCtx(), 0, null);
					vg.setBCS_ACRInputForm_IntCompany_ID(get_ID());
					vg.setBCS_ACRPositionsDict_ID(i);
					vg.setAD_Org_ID(getAD_Org_ID());
					vg.save();
				
				}	
		}
		
	return true;
}	

	protected boolean afterDelete(boolean success){
		
//		delete all child item
				
				DB.executeUpdate("delete bcs_acrinputformvgoo2 WHERE BCS_ACRInputForm_IntCompany_ID ="  +  get_ID());
				DB.executeUpdate("delete bcs_acrinputformvgoo3 WHERE BCS_ACRInputForm_IntCompany_ID ="  +  get_ID());
//				DB.executeUpdate("delete BCS_ACRInputFormAppend3_1 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
//				DB.executeUpdate("delete BCS_ACRInputFormAppend3_2 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
//				DB.executeUpdate("delete BCS_ACRInputFormAppend3_3 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
//				DB.executeUpdate("delete BCS_ACRInputFormAppend4 WHERE BCS_ACRInputForm_Company_ID ="  +  get_ID());
				
		
		return true;
		
	}

}
